﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Having.Site.Base.comm
{
    /// <summary>
    /// 自动选择时间序列化
    /// </summary>
    public class DateTimeConverter : DateTimeConverterBase
    {
        private IsoDateTimeConverter dtConverter;
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            dtConverter = existingValue.ToString().Length > 10 ? (new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }) : (new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" });
            return dtConverter.ReadJson(reader, objectType, existingValue, serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter = value.ToString().Length > 10 ? (new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }) : (new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" });
            dtConverter.WriteJson(writer, value, serializer);
        }
    }
    /// <summary>
    /// 长时间模式
    /// </summary>
    public class LongDateTimeConverter : DateTimeConverterBase
    {
        private IsoDateTimeConverter dtConverter;
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            return dtConverter.ReadJson(reader, objectType, existingValue, serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            dtConverter.WriteJson(writer, value, serializer);
        }
    }
    /// <summary>
    /// 短时间模式
    /// </summary>
    public class ShortDateTimeConverter : DateTimeConverterBase
    {
        private IsoDateTimeConverter dtConverter;
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };
            return dtConverter.ReadJson(reader, objectType, existingValue, serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };
            dtConverter.WriteJson(writer, value, serializer);
        }
    }
    /// <summary>
    /// 时分秒模式
    /// </summary>
    public class HourDateTimeConverter : DateTimeConverterBase
    {
        private IsoDateTimeConverter dtConverter;
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "HH:mm:ss" };
            return dtConverter.ReadJson(reader, objectType, existingValue, serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter = new IsoDateTimeConverter { DateTimeFormat = "HH:mm:ss" };
            dtConverter.WriteJson(writer, value, serializer);
        }
    }
}
